#include <iostream>
using namespace std;
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <memory>
#include "TcpServer.hpp"


void Usage(char* proc)
{
    cout << "Usage:\n\t" << proc << "  server_port" << endl;
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERROR);
    }
    int n = daemon(1, 1);
    if (n < 0)
    {
        cout << "daemon failed" << endl;
        exit(0);
    }
    // if (fork() > 0) exit(0);
    // int sid = setsid();
    // if (sid < 0)
    // {
    //     cout << __LINE__ << " : set sid error" << endl;
    //     exit(1);
    // }

    uint16_t port = stoi(argv[1]);
    unique_ptr<TcpServer> tsvr = make_unique<TcpServer>(port);
    tsvr->InitServer();
    tsvr->Start();

    return 0;
}